package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.itheima.dao.TagMapper;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Tag;
import com.itheima.service.AdminTagService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.UuidUtil;
import com.itheima.vo.TagVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class AdminTagServiceImpl implements AdminTagService {

    @Autowired
    private TagMapper tagMapper;

    /**
     * 增加标签
     *
     * @param tagVo
     */
    @Override
    public void add(TagVo tagVo) {
        Tag tag = new Tag();
        BeanUtils.copyProperties(tagVo, tag);
        tag.setUid(UuidUtil.getUuid());
        tag.setCreateTime(DateUtils.getNowDate().toString());
        tag.setUpdateTime(DateUtils.getNowDate().toString());
        //tag.setUid(tagVo.getUid());
        tagMapper.insertSelective(tag);
    }

    /**
     * 批量删除标签
     *
     * @param tagVoList
     */
    @Override
    public void deleteBatch(List<TagVo> tagVoList) {
        if (tagVoList == null) {
            return;
        }

        for (TagVo tagVo : tagVoList) {
            Example example = new Example(Tag.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("uid", tagVo.getUid());
            tagMapper.deleteByExample(example);
        }
    }

    /**
     * 编辑标签
     *
     * @param tagVo
     */
    @Override
    public void edit(TagVo tagVo) {
        Tag tag = new Tag();
        BeanUtils.copyProperties(tagVo, tag);
        tag.setUid(tagVo.getUid());
        tag.setUpdateTime(DateUtils.getNowDate().toString());
        tagMapper.updateByPrimaryKeySelective(tag);
    }

    /**
     * 获取标签列表 根据条件进行分页查询
     * @param keyword
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageResult findAll(String keyword, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<Map> mapList = tagMapper.findAllByKeyword(keyword);

        PageResult pageResult = new PageResult();
        pageResult.setRecords(mapList);
        pageResult.setTotal((long) mapList.size());
        pageResult.setCurrent(currentPage);
        pageResult.setSize(pageSize);
        return pageResult;
    }

    /**
     * 置顶标签
     * @param tagVo
     */
    @Override
    public void stickTag(TagVo tagVo) {
        int maxSort = tagMapper.getMaxSort();
        Tag tag = new Tag();
        BeanUtils.copyProperties(tagVo,tag);
        //设置比最大值+1
        tag.setSort(maxSort+1);
        tagMapper.updateByPrimaryKeySelective(tag);
    }

    /**
     * 通过引用量排序标签
     */
    @Override
    public void getTagSortByCite() {
        List<Tag> tagSortByCite = tagMapper.getTagSortByCite();

    }

    /**
     * 通过点击量排序标签
     */
    @Override
    public void getTagSortByClickCount() {
        tagMapper.getTagSortByClickCount();
    }
}
